import {Suspense} from "react";
import {useRoutes} from "react-router-dom";
import routes from './routes.js'
// 将自己写的routers处理成react-router-dom需要的对象
const generateRouter = (router)=>{
    return router.map((route)=>{
        if(route.children){
            route.children = generateRouter(route.children);
        }
        route.element = (
            <Suspense fallback={
                <div>加载中...</div>
            }>
                <route.component _meta={route.meta}></route.component>
            </Suspense>
        )
        return route;
    })
}
const getMeta = (path,routes)=>{
    let meta = null;
    const findMeta = (path,routes)=>{
        if(meta !== null)return;
        for(const item of routes){
            if(item.path === path){
                meta = item.meta;
                return;
            }
            if(item.children && item.children.length>0){
                findMeta(path,item.children)
            }
        }
    }
    findMeta(path,routes)
    return meta;
}
const Router = () => useRoutes(generateRouter(routes));
export {
    Router,
    getMeta,
    routes
}